<?php

namespace Apps\Common\Models;

/**
 * 问题答案的赞记录Model
 * @author     chenxiaolin
 * @date        2016年1月19日
 * @version     2.0.0
 * @copyright   Copyright 2016 meelier.com
 */
class QuestionAnswerFollow extends \PhalconPlus\Database\Model
{

    /**
     *
     * @var integer
     */
    public $answer_follow_id;

    /**
     *
     * @var integer
     */
    public $answer_id;

    /**
     *
     * @var integer
     */
    public $user_id;

    /**
     *
     * @var string
     */
    public $follow_addtime;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->belongsTo('answer_id', 'Apps\Common\Models\QuestionAnswer', 'answer_id', array('alias' => 'QuestionAnswer'));
        $this->belongsTo('user_id', 'Apps\Common\Models\UserBase', 'user_id', array('alias' => 'UserBase'));
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'question_answer_follow';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return QuestionAnswerFollow[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return QuestionAnswerFollow
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * Independent Column Mapping.
     * Keys are the real names in the table and the values their names in the application
     *
     * @return array
     */
    public function columnMap()
    {
        return array(
            'answer_follow_id' => 'answer_follow_id',
            'answer_id' => 'answer_id',
            'user_id' => 'user_id',
            'follow_addtime' => 'follow_addtime'
        );
    }

    public function defaultColumns()
    {
        return [
            'follow_addtime',
        ];
    }

    /**
     * 问答回复是否点赞
     * @param int $user_id 用户id
     * @param int $answer_id 回复id
     * @return bool
     */
    public function is_support($user_id, $answer_id)
    {
        return self::count("user_id = {$user_id} and answer_id = {$answer_id}") > 0 ? true : false;
    }
}
